//
//  ScopeConvertViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/24.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopeConvertViewController.h"
#import <MJRefresh.h>
#import "LineTransferTableViewCell.h"
#import "ScopeConverHistoryViewController.h"
#import "ScopeConvertDetailViewController.h"
#define IntConverString(value1)  [NSString stringWithFormat:@"%d",value1];


static NSString *cellIdentifier = @"lineTransfer";

@interface ScopeConvertViewController ()

@end

@implementation ScopeConvertViewController

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = YES;
}

 

- (void)viewDidLoad {
    [super viewDidLoad];
    _type = @"0";
    _currenPage = 1;
    _serviceCurrenPage = 1;
    [self initAbout];
    [self netRequst];
}

- (void)netRequst {
    
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    _carAround.converType = _type;
    _carAround.page = @"1";
    [_carAround getScopeExchange:^(id result) {
        if([self typeIsOilCar]){
            _tableDataSource = [NSMutableArray arrayWithArray:[result customObjectForkey:@"pointype"]];

        }else{
            _serviceTableDataSource = [NSMutableArray arrayWithArray:[result customObjectForkey:@"pointype"]];
        }
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
    
}

- (void)initDataSource {
    if ([self typeIsOilCar]) {
        if (_tableDataSource.count <= 0) {
            return;
        }
    }else{
        if (_serviceTableDataSource.count <= 0) {
            return;
        }
    }
   
    if(!_tableView.dataSource){
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    [_tableView reloadData];
}

- (void)initAbout {
    self.title = @"积分兑换";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:@selector(backTo) andCustom:NO];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"历史记录" forState:UIControlStateNormal];
    btn.adjustsImageWhenHighlighted = NO;
    [btn setImage:[UIImage imageNamed:@"lishijilu"] forState:UIControlStateNormal];
    [btn setFont:[UIFont systemFontOfSize:13.0f]];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setFrame:CGRectMake(0, 0, 80, 50)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LineTransferTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    self.tableView.tableFooterView = [UIView new];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)backTo {
     self.tabBarController.tabBar.hidden = NO;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)footerRefresh {
    if ([self typeIsOilCar]) {
        _currenPage ++;
        _carAround.page = IntConverString(_currenPage);
     }else {
        _serviceCurrenPage ++;
        _carAround.page = IntConverString(_serviceCurrenPage);
    }
    _carAround.converType = _type;
    [_carAround getScopeExchange:^(id result) {
        NSArray *arr = [NSMutableArray arrayWithArray:[result customObjectForkey:@"pointype"]];
        if ([self typeIsOilCar]) {
            [_tableDataSource addObjectsFromArray:arr];
        }else {
            [_serviceTableDataSource addObjectsFromArray:arr];
        }
        [self initDataSource];
        [self.tableView footerEndRefreshing];
    } anfFail:^(id result) {
        [self typeIsOilCar] ?  _currenPage -- : _serviceCurrenPage --;
        [self.tableView footerEndRefreshing];
    }];
}

- (void)btnAction:(UIButton *)sender {
    //TODO:跳转到历史纪录
    ScopeConverHistoryViewController *table = [[ScopeConverHistoryViewController alloc] initWithNibName:@"ScopeConverHistoryViewController" bundle:nil];
    [self.navigationController pushViewController:table animated:YES];
}

- (void)changeNetWork {
    if ([self typeIsOilCar]) {
        NSLog(@"%ld",_currenPage);
        if (!_tableDataSource) {
            [self netRequst];
        }else{
            [self.tableView reloadData];
        }
    }else {
        NSLog(@"%ld",_serviceCurrenPage);

        if (!_serviceTableDataSource) {
            [self netRequst];
        }else{
            [self.tableView reloadData];

        }
    }
}
 
//线上兑换300
//线下兑换301
- (IBAction)onlineTransferAction:(UIButton *)sender {
    if (sender.tag == 300) { //线上兑换
        _type = @"0";
        _onlineTransfer.selected = YES;
        _offlineTransfer.selected = NO;
         if (_line.frame.origin.x != 0) {
            [UIView animateWithDuration:.3f animations:^{
                _line.frame = CGRectMake(0, _line.frame.origin.y, _line.frame.size.width, _line.frame.size.height);
            }];
        }
    }else{                   //线下兑换
        _type = @"1";
        _onlineTransfer.selected = NO;
        _offlineTransfer.selected = YES;
        [UIView animateWithDuration:.3f animations:^{
                _line.frame = CGRectMake( _line.frame.size.width, _line.frame.origin.y, _line.frame.size.width, _line.frame.size.height);
        }];
    }
    [self changeNetWork];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - tableView Delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self typeIsOilCar]) {
        return _tableDataSource.count > 0 ? _tableDataSource.count : 0;
    }else{
        return _serviceTableDataSource.count > 0 ? _serviceTableDataSource.count : 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LineTransferTableViewCell *line = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if(line.info){
        line.info = nil;
    }
    if ([self typeIsOilCar]) {
        line.info = [[_tableDataSource objectAtIndex:indexPath.row] copy];
    }else{
        line.info = [[_serviceTableDataSource objectAtIndex:indexPath.row] copy];
    }
    return line;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ScopeConvertDetailViewController *detail = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"scopeDetail"];
    detail.type = [_type integerValue];
    if ([_type isEqualToString:@"0"]) {
         detail.goodId = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ID"];
     }else{
         detail.goodId = [[_serviceTableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ID"];
     }
    [self.navigationController pushViewController:detail animated:YES];
}

- (BOOL)typeIsOilCar{
    if ([_type isEqualToString:@"0"]) {
        return YES;
    }else{
        return NO;
    }
}

@end
